package com.wu.controller;


import com.wu.pojo.User;
import com.wu.service.UserService;
import com.wu.vo.VoUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "用户管理相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询一个
    @ApiOperation("根据id查询用户的接口 redis+mybatis-plus")
    @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "2", required = true)
    @RequestMapping("/findone/{id}")
    public VoUser findById(@PathVariable("id") int id){
        User user = (User) redisTemplate.opsForValue().get("user_" + id);
        if(user==null){
            user = userService.selectById(id);
            redisTemplate.opsForValue().set("user_"+id,user,20, TimeUnit.SECONDS);

        }
        //简单写写 就这个思路而已
        VoUser voUser = new VoUser();
        voUser.setId(user.getId()).setName(user.getName()).setAge(user.getAge()).setMsg("中国牛逼");
        return voUser;

    }

    //更新一个
    @ApiOperation("根据id更新用户的接口 mybatis-plus")
    @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "4", required = true)
    @RequestMapping("/updateone/{id}")
    public int updateById(@PathVariable("id") int id){
        return userService.updateById(userService.selectById(id).setName("开心猫").setAge(123));

    }

    //查询全部
    @ApiOperation("查询所有用户的接口 XML配置")
    @RequestMapping("/findall")
    public List<User> updateById(){
        return userService.findAll();
    }

    //测试接口注解
    @ApiOperation("根据id查询用户的接口 注解配置")
    @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "2", required = true)
    @RequestMapping("/findstudent/{id}")
    public User findstudent(@PathVariable("id") int id){
        return userService.findstudent(id);
    }

}
